<template>
  <div class="classifySong-index">
    <div id="classifySong" @click="handleBlock">
      <!-- header -->
      <div class="classifyContainer">
        <div class="classify">
          <h3 class="classifyTitle">语种</h3>
          <ul class="classifyList">
            <li class="classifyItem" v-for="item in languageClassify" :key="item.id">
              <a @click="getSongList(1, item.name)">{{ item.name }}</a>
            </li>
          </ul>
        </div>
        <div class="classify">
          <h3 class="classifyTitle">流派</h3>
          <ul class="classifyListStyle">
            <li class="classifyItem" v-for="item in styleClassify" :key="item.id">
              <a href="#" @click="getSongList(1, item.name)">{{ item.name }}</a>
            </li>
            <li class="classifyItem" ref="more">
              <a class="more" @click="showMore">
                更多
                <span class="iconfont el-icon-arrow-down"></span>
              </a>
            </li>
          </ul>
        </div>
        <div class="classify">
          <h3 class="classifyTitle">主题</h3>
          <ul class="classifyList">
            <li class="classifyItem" v-for="item in subjectClassify" :key="item.id">
              <a @click="getSongList(1, item.name)">{{ item.name }}</a>
            </li>
          </ul>
        </div>
        <div class="classify">
          <h3 class="classifyTitle">心情</h3>
          <ul class="classifyList">
            <li class="classifyItem" v-for="item in emotionClassify" :key="item.id">
              <a @click="getSongList(1, item.name)">{{ item.name }}</a>
            </li>
          </ul>
        </div>
        <div class="classify">
          <h3 class="classifyTitle">场景</h3>
          <ul class="classifyList">
            <li class="classifyItem" v-for="item in sceneClassify" :key="item.id">
              <a @click="getSongList(1, item.name)">{{ item.name }}</a>
            </li>
          </ul>
        </div>
      </div>

      <div class="showMore" :style="{ display: isShowMore }" ref="showmore">
        <ul class="showMoreList">
          <li class="showMoreItem" v-for="item in moreStyleClassify" :key="item.id">
            <a @click="getSongList(1, item.name)">{{ item.name }}</a>
          </li>
        </ul>
      </div>

      <!-- recommend -->
      <div class="recommendContainer">
        <h2 v-if="!tagName">全部歌单</h2>
        <h2 v-else class="tag">
          <span>{{ tagName }}</span>
          <a @click="hideShowTag">x</a>
        </h2>

        <div>
          <a
            class="recommendAntd recommendAntd-left"
            :class="isSelected ? 'selected' : ''"
            @click="getSongList('a')"
          >推荐</a>
          <a
            class="recommendAntd recommendAntd-right"
            :class="isSelected ? '' : 'selected'"
            @click="getHotSongList"
          >最新</a>
        </div>
      </div>

      <!-- List -->
      <ul class="listContainer">
        <li class="list" v-for="item in playList" :key="item.id">
          <div class="imgList img">
            <a>
              <img v-lazy="item.coverImgUrl" @click="toDetail(item.id)" />
              <i class="mask"></i>
              <i class="bofang"></i>
            </a>
          </div>
          <a>{{ item.name }}</a>
          <div class="author">{{ item.creator.nickname }}</div>
          <div class="count">播放量：{{ (item.playCount / 10000).toFixed(1) }}万</div>
        </li>
      </ul>

      <!-- bottom -->
      <div class="bottom">
        <p>查看更多内容，请下载客户端</p>
        <a href="#" class="bottom-btn">立刻下载</a>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getSongClassify,
  getSongList,
  getSongHotList,
} from "../../api/classifySong";

export default {
  name: "classifySong",
  data() {
    return {
      languageClassify: [],
      styleClassify: [],
      sceneClassify: [],
      emotionClassify: [],
      subjectClassify: [],
      moreStyleClassify: [],

      isShowMore: "none",

      playList: [],

      isSelected: true,

      tagName: "",
    };
  },
  async mounted() {
    // 获取歌曲分类信息
    const { tags } = await getSongClassify();
    this.languageClassify = tags.filter((item) => {
      return item.category === 0;
    });
    let styleClassify = tags.filter((item) => {
      return item.category === 1;
    });
    this.styleClassify = styleClassify.splice(0, 8);
    this.moreStyleClassify = styleClassify;

    this.sceneClassify = tags.filter((item) => {
      return item.category === 2;
    });
    this.emotionClassify = tags.filter((item) => {
      return item.category === 3;
    });
    this.subjectClassify = tags.filter((item) => {
      return item.category === 4;
    });

    // 获取精品歌单
    this.getSongList();
  },
  methods: {
    // 展示分类的二级菜单
    showMore() {
      if (this.isShowMore === "none") {
        this.isShowMore = "block";
      } else {
        this.isShowMore = "none";
      }
    },
    // 获取热门歌曲
    async getHotSongList() {
      const { playlists } = await getSongHotList();
      this.playList = playlists;
      this.isSelected = false;
    },

    // 获取精品歌单
    async getSongList(e, name) {
      const { playlists } = await getSongList(name);
      this.playList = playlists;
      this.isSelected = true;
      this.tagName = name;
    },

    // 隐藏tag
    hideShowTag() {
      this.tagName = "";
      this.getSongList();
    },

    // 点击空白区域隐藏二级菜单
    handleBlock(e) {
      // console.log(e.target);
      const showmore = this.$refs.showmore;
      const more = this.$refs.more;

      // contains代表是否包含前面这个标签
      if (!showmore.contains(e.target) && !more.contains(e.target)) {
        this.isShowMore = "none";
      }
    },

    // 跳转到歌单详情页面
    toDetail(id) {
      this.$router.history.push({
        name: "",
        id,
      });
    },
  },
};
</script>

<style scoped>
/* header开始 */
body a {
  cursor: pointer;
}
#classifySong {
  max-width: 1250px;
  margin: 0 auto;
}
.classifySong-index {
  background-color: #fafafa;
}
.classifyContainer {
  text-align: left;
  justify-content: space-around;
  display: flex;
  padding: 60px 0 32px 0;
}
.language {
  /* width: 203px; */
  height: 116px;
}
.classifyTitle {
  color: #999;
}
.classifyList {
  width: 183px;
  height: 90px;
  margin-right: 20px;
  margin-top: 5px;
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  border-right: 1px solid #eeeef0;
}
.classifyListStyle {
  width: 238px;
  height: 90px;
  margin-right: 20px;
  margin-top: 5px;
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  border-right: 1px solid #eeeef0;
}
.classifyItem {
  width: 76px;
  height: 26px;
  line-height: 26px;
  font-size: 14px;
  margin-bottom: 4px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.classifyItem a {
  height: 26px;
  text-decoration: none;
  padding: 0 8px;
  color: #000;
  cursor: pointer;
}
.classifyItem a:hover {
  color: #31c27c;
}
.classifyItem .more {
  height: 26px;
  line-height: 26px;
}

/* showmore开始 */
.showMore {
  position: absolute;
  top: 315px;
  width: 1200px;
  height: 30px;
  line-height: 30px;
  padding: 19px 0 15px 0;
  background-color: #fff;
  box-shadow: 0 0 10px hsl(0deg 0% 72% / 65%);
}
.showMoreList {
  display: flex;
  margin-left: 20px;
}
.showMoreItem {
  margin-right: 32px;
  margin-bottom: 4px;
}
.showMoreItem a {
  padding: 0 8px;
  overflow: hidden;
  font-size: 14px;
  color: #000;
}
.showMoreItem a:hover {
  color: #31c27c;
}

/* recommend开始 */
.recommendContainer {
  width: 1200px;
  height: 60px;
  line-height: 60px;
  display: flex;
  justify-content: space-between;
}
.recommendContainer h2 {
  font-size: 24px;
  color: #000;
}
.recommendContainer .tag {
  height: 38px;
  line-height: 38px;
  padding: 0 10px 0 10px;
  border: 1px solid #c9c9c9;
  font-size: 14px;
  color: #000;
}
.recommendContainer .tag:hover {
  background-color: #37c27c;
  color: #fff;
}
.recommendContainer .tag span {
  margin-right: 5px;
}
.recommendContainer .tag a {
  width: 16px;
  height: 16px;
}
.recommendAntd {
  display: inline-block;
  height: 40px;
  line-height: 40px;
  padding: 0 16px;
  border: 1px solid #c9c9c9;
  /* border-radius: 2px; */
  cursor: pointer;
  font-size: 14px;
}
.recommendAntd-left {
  right: -1px;
  border-top-left-radius: 2px;
  border-bottom-left-radius: 2px;
}
.recommendAntd-right {
  border-top-right-radius: 2px;
  border-bottom-right-radius: 2px;
}
.selected {
  background-color: #31c27c;
  color: #fff;
}

/* List开始 */
.listContainer {
  /* height: 1357px; */
  display: flex;
  flex-wrap: wrap;
}
.imgList {
  margin-bottom: 15px;
}
.imgList img {
  width: 228px;
  height: 228px;
  cursor: pointer;
}
.list {
  width: 228px;
  margin-right: 20px;
  padding-bottom: 44px;
}
.list a {
  font-size: 14px;
  color: #000;
}
.author {
  height: 22px;
  line-height: 22px;
  font-size: 14px;
  color: #999;
}
.count {
  height: 22px;
  line-height: 22px;
  font-size: 14px;
  color: #999;
}

/* bottom */
.bottom {
  text-align: center;
  padding-bottom: 80px;
}
.bottom p {
  font-size: 18px;
  color: #000;
}
.bottom-btn {
  display: block;
  width: 175px;
  height: 41px;
  line-height: 41px;
  font-size: 16px;
  border-radius: 41px;
  color: #fff;
  margin: 20px auto 0;
  background-color: #31c27c;
}
.bottom-btn:hover {
  background-color: #2caf6f;
}

/*遮罩层 */
.imgList {
  position: relative;
}
.imgList .bofang {
  transform: scale(0.7) translateZ(0);
}
.imgList:hover .bofang {
  opacity: 1;
  transition: opacity 0.5s;
  transform: scale(1) translateZ(0);
  transition-property: opacity, transform;
  transition-duration: 0.5s;
}
.imgList:hover .mask {
  opacity: 0.2;
  transition: opacity 0.5s;
}
</style>